class MembersController < ApplicationController
  layout "application"
  #helper :Images

  # GET /members
  # GET /members.xml
  def index
    @members = Member.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @members }
    end
  end

  # GET /members/1
  # GET /members/1.xml
  def show
    @member = Member.find(params[:id])
    @member.password = ""
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @member }
    end
  end

  # GET /members/new
  # GET /members/new.xml
  def new
    @member = Member.new

    respond_to do |format|
      format.html #{render :layout=>"application"}# new.html.erb
      format.xml  { render :xml => @member }
    end    
  end

  # GET /members/1/edit
  def edit
    @member = Member.find(params[:id])
    @member.password = ""
  end

  # POST /members
  # POST /members.xml
  def create
    @member = Member.new(params[:member])
    @member.joindate = Date.today.to_s
    @member.password = digest_enc(@member.password)
    if(params[:head_photo]) then
      begin
        @image = Image.new()
        @image[:path],@image[:photo_type],@image[:path]=uploadFile(params[:head_photo])
        @image[:is_deleted] = 'n'
        @image[:create_date] = date_time()
        @member.image = @image
        if @image.save then
          flash[:notice] = 'head_photo was successfully saved!'
        end
      rescue =>e
        flash[:notice] = e
      # else
      # ensure
      end
    end

    respond_to do |format|
      if @member.save
        #flash[:notice] = 'Member was successfully created.'
        format.html { redirect_to(@member) }
        format.xml  { render :xml => @member, :status => :created, :location => @member }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @member.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /members/1
  # PUT /members/1.xml
  def update
    @member = Member.find(params[:id])
    temp_member = params[:member]
    temp_member[:password] = digest_enc(params[:member][:password])
    respond_to do |format|
      if @member.update_attributes(temp_member)
        flash[:notice] = 'Member was successfully updated.'
        format.html { redirect_to(@member) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @member.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /members/1
  # DELETE /members/1.xml
  def destroy
    @member = Member.find(params[:id])
    @member.destroy

    respond_to do |format|
      format.html { redirect_to(members_url) }
      format.xml  { head :ok }
    end
  end
end
